using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ImportModelFromRhino
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ModelManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public List<CustomModel> models = new List<CustomModel>();
        Camera camera;
        CameraNumPad cameraNumPad;
        Viewport viewport;
        Game game;

        public ModelManager(Game game, Camera camera, Viewport viewport)
            : base(game)
        {
            this.camera = camera;
            this.viewport = viewport;
            this.game = game;
            // TODO: Construct any child components here
        }

        public ModelManager(Game game, CameraNumPad camera, Viewport viewport)
            : base(game)
        {
            this.cameraNumPad = camera;
            this.viewport = viewport;
            this.game = game;
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Aggiunge un elemento alla lista di modelli da elaborare
        /// </summary>
        /// <param name="model"></param>
        public void add(String modelName)
        {
            models.Add(Game.Content.Load<CustomModel>(modelName));
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            game.GraphicsDevice.Viewport = viewport;
            if (camera != null)
                drawScene(gameTime, camera);
            else
                drawScene(gameTime, cameraNumPad);
        }

        protected void drawScene(GameTime gameTime, CameraNumPad camera)
        {
            foreach (CustomModel model in models)
            {
                foreach (ModelMesh mesh in model.model.Meshes)
                {
                    foreach (BasicEffect bsx in mesh.Effects)
                    {
                        bsx.World = camera.world;
                        bsx.View = camera.view;
                        bsx.Projection = camera.projection;
                        bsx.EnableDefaultLighting();
                    }
                    mesh.Draw();
                }
                base.Draw(gameTime);
            }
        }

        protected void drawScene(GameTime gameTime, Camera camera)
        {
            foreach (CustomModel model in models)
            {
                foreach (ModelMesh mesh in model.model.Meshes)
                {
                    foreach (BasicEffect bsx in mesh.Effects)
                    {
                        bsx.World = camera.world;
                        bsx.View = camera.view;
                        bsx.Projection = camera.projection;
                        bsx.EnableDefaultLighting();
                    }
                    mesh.Draw();
                }
                base.Draw(gameTime);
            }
        }
    }
}
